package DAO;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Beans.AdministracionBean;
import Beans.SucursalBean;
import HibernateUtils.HibernateUtil;

public class HibernateAdministracionDAO {
	private static HibernateAdministracionDAO instancia;
	private static SessionFactory sf;
	private HibernateAdministracionDAO(){
		
	}
	
	public static HibernateAdministracionDAO getInstancia(){
		if(instancia==null){
			sf=HibernateUtil.getSessionFactory();
			instancia=new HibernateAdministracionDAO();
		}
		return instancia;
	}
	
	public void grabarAdministracion(AdministracionBean fb){
		Session session=sf.openSession();
		session.beginTransaction();
		session.save(fb);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public AdministracionBean buscarAdministracion(AdministracionBean fb) {
		Session session=sf.openSession();
		AdministracionBean f=(AdministracionBean) session.get(AdministracionBean.class, fb.getId());
		session.close();
		return f;
	}
	
	public List<AdministracionBean> buscarAdministracionXSucursal(SucursalBean sb) {
		Session session=sf.openSession();
		session.beginTransaction();	
		@SuppressWarnings("unchecked")
		List<AdministracionBean> administraciones = (List<AdministracionBean>) session.createQuery("from AdministracionBean where idSucursal = :idSuc").setParameter("idSuc", sb.getIdSucursal());			
		session.close();
		return administraciones;
	}

	public void updateAdministracion(AdministracionBean admb) {
		Session session=sf.openSession();
		session.beginTransaction();
		session.update(admb);
		session.flush();
		session.getTransaction().commit();
		session.close();		
	}
}
